package io.github.biezhi.elves.spider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.github.biezhi.elves.credit.model.CompNameList;
import io.github.biezhi.elves.credit.model.CreditReport;
import io.github.biezhi.elves.ok.http.FastHttpClient;
import io.github.biezhi.elves.qcc.model.CompBidUrlDate;
import io.github.biezhi.elves.qcc.model.CompBidUrlResolve;
import io.github.biezhi.elves.qcc.model.CompBidding;
import io.github.biezhi.elves.service.CreditReportService;
import io.github.biezhi.elves.service.QccBidUrlDateService;
import io.github.biezhi.elves.service.QccBidUrlResolveService;
import io.github.biezhi.elves.utils.StringUtils;

 /**
  * 爬取信用广东信用中国
  * @author Administrator
  *
  */
@Component
public class CreditReportSpider {
	
	@Autowired
	private CreditReportService creditReportService;
	
//	private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
	private final static Executor executor = Executors.newFixedThreadPool(8);//启用多线程
	
	//爬取企查查公告基本信息到数据库
	public void spiderCreditReportToDB() throws Exception{
		
		//查询公告基本信息
		List<CompNameList> compNameList = creditReportService.queryCompNameList(500000);
		
		//关键是这一句代码，将 i 转化为  j，这样j 还是final类型的参与线程
		for(int i=0;i<compNameList.size();i++){
				final int j=i;                                                                                                                                  
				executor.execute(new Runnable() {
		             @Override
		             public void run() {
		              try{
		            	  CompNameList compinfo = compNameList.get(j);
		
		  				//企业id
		  				String compId = compinfo.getCompId();
		  				//企业名称
		  				String compName = compinfo.getCompName();
		  				
		  				//根据企业名称爬取页面
		  				String creditGdHtml = spiderCreditGd(compName);
		  				
		  				//解析获取公告发表时间，以及公告源地址
		  				Document doc = Jsoup.parse(creditGdHtml);
		  				String notfindstr = doc.select("div.list_content.not_find_list").text();
		  				CreditReport creditReport = new CreditReport();
		  				if (notfindstr!=null && notfindstr.contains("找不到和您的查询相符的记录")) {
							//找不到该企业的信用报告
		  					creditReport.setCompId(compId);
		  					creditReport.setCompName(compName);
		  					creditReport.setCreditReportUrl("");
		  					creditReport.setRequestType("");
		  					creditReport.setParamName("");
		  					creditReport.setParamValue("");
		  					creditReportService.insertCreditReport(creditReport);
		  					creditReportService.updateCompNameListState(compId, 1);
		  					
						}else {
							String creditInfoId = doc.select("#creditInfoId").val();
							if (creditInfoId!=null && creditInfoId.length()>0) {
								creditReport.setCompId(compId);
			  					creditReport.setCompName(compName);
			  					creditReport.setCreditReportUrl("https://credit.gd.gov.cn/creditreportAction!exportPDF1.do");
			  					creditReport.setRequestType("post");
			  					creditReport.setParamName("objModel.f000");
			  					creditReport.setParamValue(creditInfoId);
			  					creditReportService.insertCreditReport(creditReport);
			  					creditReportService.updateCompNameListState(compId, 1);
							}else {
								//查询出了多个结果的情况
								String href = doc.select(".list_content").get(0).select("a").attr("href");
								String[] split = href.split("=");
								String paramValue = split[1];
								
			  					creditReport.setCompId(compId);
			  					creditReport.setCompName(compName);
			  					creditReport.setCreditReportUrl("https://credit.gd.gov.cn/creditreportAction!exportPDF1.do");
			  					creditReport.setRequestType("post");
			  					creditReport.setParamName("objModel.f000");
			  					creditReport.setParamValue(paramValue);
			  					creditReportService.insertCreditReport(creditReport);
			  					creditReportService.updateCompNameListState(compId, 1);
							}
						}
		
		              }catch(Exception e){
		    				//更新为已爬取
		  					creditReportService.updateCompNameListState(compNameList.get(j).getCompId(), 2);
		              }
		             }
				});
		}
		
		System.out.println("爬取完成！");
		
	}
	
	//根据企业名称爬取页面
	private String spiderCreditGd(String compName){
		
		String result = "";
		try {
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
			okHttpResponse = FastHttpClient
					.newBuilder()
					.build()
					.get()
					.url("https://credit.gd.gov.cn/creditquery!queryLegalEntityOrgList.do?conditions="+compName)
					.build()
					.execute();
			System.out.println("https://credit.gd.gov.cn/creditquery!queryLegalEntityOrgList.do?conditions="+compName);
			result = okHttpResponse.string();
			if(!StringUtils.isNullOrEmpty(result)){
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用spiderCreditGd(biddingTitleUrl)方法===");
			result = spiderCreditGd(compName);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用spiderCreditGd(biddingTitleUrl)方法===");
			result = spiderCreditGd(compName);
		}
		return result;
		
	}
	
	public String  getSubUtilSimple(String soap,String rgex){
		Pattern pattern = Pattern.compile(rgex);// 匹配的模式
		Matcher m = pattern.matcher(soap);
		String result = "";
		ArrayList<String> resultArr = new ArrayList<>();
		while (m.find()) {
			resultArr.add(m.group(1));
		}
		if (resultArr != null && resultArr.size()>0) {
			for (String str : resultArr) {
				result += str + ",";
			}
			result = result.substring(0, result.length()-1);
		}
		return result;
	}
	
	//读取文本为字符串
	public String getXmlString() {
		String xmlString;
		char[] strBuffer = null;
		int flen = 0;
		File xmlfile = new File("E:\\test.html");
		try {
		FileInputStream input = new FileInputStream(xmlfile);
		InputStreamReader in=new InputStreamReader(input,"utf-8");//字节流字符流转化的桥梁
		flen = (int)xmlfile.length();
		strBuffer = new char[flen];
		in.read(strBuffer, 0, flen);
		} catch (FileNotFoundException e) {
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		}
		xmlString = new String(strBuffer); //构建String时，可用char[]类型，

		return xmlString;
		}
	
	public String unicodeToUtf8(String unicodeStr){
		try {
			byte[] utf8  = unicodeStr.getBytes("UTF-8");
			String utf8Str = new String(utf8,"UTF-8");
			return utf8Str;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		CreditReportSpider creditReportSpider = new CreditReportSpider();
		String creditGdHtml = creditReportSpider.getXmlString();
		Document doc = Jsoup.parse(creditGdHtml);
		String attr = doc.select(".list_content").get(0).select("a").attr("href");
		String[] split = attr.split("=");
		String str = split[1];
		System.out.println(str);
		
		
	}
}




